package adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import bean.BooksDatilBean;
import jiyun.com.yiyanmeng.R;

/**
 * Created by 裘翔 on 2019/11/8.
 */

public class BooksDatilAdaper extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private ArrayList<BooksDatilBean.ShopBean> banners;
    private ArrayList<BooksDatilBean.ShopBean> list;
    private List<String> feng_pic;

    public BooksDatilAdaper(Context context, ArrayList<BooksDatilBean.ShopBean> banners, ArrayList<BooksDatilBean.ShopBean> list) {
        this.context = context;
        this.banners = banners;
        this.list = list;

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            View view0 = View.inflate(context, R.layout.item_banner_shop, null);
            return new ViewHolder0(view0);
        } else {
            View view1 = View.inflate(context, R.layout.item_datil_books, null);
            return new ViewHolder1(view1);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int type = getItemViewType(position);
        if (type==0){
            ViewHolder0 holder0= (ViewHolder0) holder;
            feng_pic = banners.get(position).getFeng_pic();
            holder0.bx_banner.setImages(feng_pic).setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    //BooksDatilBean.ShopBean _banner = (BooksDatilBean.ShopBean) path;
                    String s = (String) path;
                    Glide.with(context).load(s).into(imageView);
                }
            }).start();
        }else {
            ViewHolder1 holder1= (ViewHolder1) holder;
            if (banners.size()>0){
                position=position-1;
            }
            holder1.bk_name.setText(list.get(position).getName());
            holder1.bk_small.setText(list.get(position).getXian_price());
            holder1.bk_big.setText(list.get(position).getYuan_price());
            holder1.bk_address.setText(list.get(position).getFa_huo_di());
            holder1.bk_yuexiao.setText(list.get(position).getYue_shou());
        }
    }

    @Override
    public int getItemCount() {
        if (banners.size()>0){
            return list.size()+1;
        }else {
            return list.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (banners.size()>0&&position==0){
            return 0;
        }else {
            return 1;
        }
    }

    class ViewHolder0 extends RecyclerView.ViewHolder {
        Banner bx_banner;
        public ViewHolder0(View view0) {
            super(view0);
            bx_banner=itemView.findViewById(R.id.bk_banner);
        }
    }

    class ViewHolder1 extends RecyclerView.ViewHolder {
        TextView bk_name;
        TextView bk_small;
        TextView bk_big;
        TextView bk_yuexiao;
        TextView bk_address;
        public ViewHolder1(View view1) {
            super(view1);
            bk_name=itemView.findViewById(R.id.bk_name);
            bk_address=itemView.findViewById(R.id.bk_address);
            bk_small=itemView.findViewById(R.id.bk_small);
            bk_big=itemView.findViewById(R.id.bk_big);
            bk_yuexiao=itemView.findViewById(R.id.bk_yuexiao);
        }
    }


}
